package org.linlinjava.litemall.admin.web;


import org.linlinjava.litemall.admin.vo.CatVo;
import org.linlinjava.litemall.core.util.ResponseUtil;
import org.linlinjava.litemall.core.validator.Order;
import org.linlinjava.litemall.core.validator.Sort;
import org.linlinjava.litemall.db.domain.LitemallFeedback;
import org.linlinjava.litemall.db.domain.LitemallWebArticle;
import org.linlinjava.litemall.db.domain.LitemallWebCategory;
import org.linlinjava.litemall.db.domain.LitemallWebProduct;
import org.linlinjava.litemall.db.service.LitemallFeedbackService;
import org.linlinjava.litemall.db.service.LitemallWebArticleService;
import org.linlinjava.litemall.db.service.LitemallWebCategoryService;
import org.linlinjava.litemall.db.service.LitemallWebProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/web/api")
@Validated
public class WebApiController {
    @Autowired
    private LitemallFeedbackService feedbackService;
    @Autowired
    private LitemallWebArticleService webArticleService;
    @Autowired
    private LitemallWebCategoryService webCategoryService;
    @Autowired
    private LitemallWebProductService webProductService;

    @PostMapping("/getindexarticles")
    public Object getIndexArticles(Integer count){
        List<LitemallWebArticle> allArticle = webArticleService.getIndexArticle();
        if(count!=null&&count>0&&count<allArticle.size()){
            allArticle=allArticle.subList(0,count);
        }
        return ResponseUtil.okList(allArticle);
    }
    @RequestMapping("/getarticles")
    public Object getArticles(@RequestParam Integer cid, @RequestParam Integer curPage, @RequestParam Integer limit,@Sort @RequestParam(defaultValue = "add_time") String sort,
                              @Order @RequestParam(defaultValue = "desc") String order){

        List<LitemallWebArticle> list = webArticleService.list(null, null,cid, curPage, limit, sort, order);
        return ResponseUtil.okList(list);
    }
    @RequestMapping("/getwebcategory")
    public Object getWebCategory(){
        List<LitemallWebCategory> l1CatList = webCategoryService.queryL1ForType("0");
        return list2(l1CatList);
    }
    public Object list2(List<LitemallWebCategory> l1CatList) {
        List<CatVo> categoryList = new ArrayList<>(l1CatList.size());
        for (LitemallWebCategory l1 : l1CatList) {
            CatVo l1CatVo = new CatVo();
            l1CatVo.setValue(l1.getId());
            l1CatVo.setLabel(l1.getName());

            List<LitemallWebCategory> l2CatList = webCategoryService.queryByPid(l1.getId());
            if(org.apache.commons.collections.CollectionUtils.isNotEmpty(l2CatList)) {
                List<CatVo> children = new ArrayList<>(l2CatList.size());
                for (LitemallWebCategory l2 : l2CatList) {
                    CatVo l2CatVo = new CatVo();
                    l2CatVo.setValue(l2.getId());
                    l2CatVo.setLabel(l2.getName());
                    children.add(l2CatVo);
                }
                l1CatVo.setChildren(children);
            }
            categoryList.add(l1CatVo);
        }
        Map<String, Object> data = new HashMap<>();
        data.put("categoryList",categoryList);
        return ResponseUtil.ok(data);
    }

    @RequestMapping("/getwebproductlist")
    public Object getWebProductList(Integer companyId){
        List<LitemallWebProduct> productList = webProductService.selectList();
        Map<String, Object> data = new HashMap<>();
        data.put("productList",productList);
        return ResponseUtil.ok(data);
    }
    @RequestMapping("/getwebproductinfo")
    public Object getWebProductinfo(Integer pid){
        LitemallWebProduct product = webProductService.findById(pid);
        Map<String, Object> data = new HashMap<>();
        data.put("product", product);
        return ResponseUtil.ok(data);
    }
    @RequestMapping("/getarticleinfo")
    public Object getArticleinfo(Integer aid){
        LitemallWebArticle article = webArticleService.findById(aid);
        article.setClickCount(article.getClickCount()+1);
        webArticleService.updateById(article);
        Map<String, Object> data = new HashMap<>();
        data.put("article", article);
        return ResponseUtil.ok(data);
    }
    @RequestMapping("/savemessage")
    public Object save(@RequestBody LitemallFeedback feedback){
        feedback.setFeedType("网站留言");
        feedbackService.add(feedback);
        return ResponseUtil.ok();
    }
}
